﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComFun;

namespace KAImage.Config
{
    /// <summary>
    /// 色阶调整选项
    /// </summary>
    [Serializable]
    [XmlRootAttribute("ImageLevelsOption", Namespace = "KAImage.Config", IsNullable = false)]
    public class ImageLevelsOption : XmlSerializeBase
    {     
        /// <summary>
        /// 全通道的色阶调整选项
        /// </summary>
        public ChannelLevelOption RGB { get; set; }
      
        /// <summary>
        /// 红色单通道的色阶调整选项
        /// </summary>
        public ChannelLevelOption R { get; set; }
       
        /// <summary>
        /// 绿色单通道的色阶调整选项
        /// </summary>
        public ChannelLevelOption G { get; set; }
        
        /// <summary>
        /// 蓝色单通道的色阶调整选项
        /// </summary>
        public ChannelLevelOption B { get; set; }
        public ImageLevelsOption()
        {
            RGB = new ChannelLevelOption();
            R = new ChannelLevelOption();
            G = new ChannelLevelOption();
            B = new ChannelLevelOption();
        }

        /// <summary>
        /// 指示选项是否是默认值，一般对于不是默认值才进行调整
        /// </summary>
        public bool IsDefault
        {
            get
            {
                return (RGB.IsDefault 
                    && R.IsDefault 
                    && G.IsDefault 
                    && B.IsDefault);
            }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            ImageLevelsOption obj = (ImageLevelsOption)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this.RGB = obj.RGB;
                this.R = obj.R;
                this.G = obj.G;
                this.B = obj.B;
                this._nVer = obj._nVer;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {          
            StringBuilder builder = new StringBuilder("图像色阶调整选项: ");
            FillFieldValueStringDelegate FillString =
               (string field, object val) =>
               {
                   if (val != null)
                   {
                       builder.Append("[");
                       builder.Append(field);
                       builder.Append("]: ");
                       builder.Append(val.ToString() + "; ");
                   }
               };
            FillString("[RGB通道]:", RGB);
            FillString("[R通道]:", R);
            FillString("[G通道]:", G);
            FillString("[B通道]:", B);
            return builder.ToString();
        }
    }

    public class ChannelLevelOption
    {
        public double MidTone { get; set; }
        public ChannelLevelOption()
        {
            MidTone = 1.00D;
        }

        /// <summary>
        /// 指示选项是否是默认值，一般对于不是默认值才进行调整
        /// </summary>
        public bool IsDefault
        {
            get 
            {
                return MidTone.Equals(1.00D);
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("通道色阶调整选项: ");
            FillFieldValueStringDelegate FillString =
                (string field, object val) =>
                {
                    if (val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }
                };
            FillString("中间调", MidTone);
            return builder.ToString();
        }
    }

}
